﻿using System.Threading.Tasks;
using System.Xml.Linq;

using SeriesManager.Enum;
using System.Collections.ObjectModel;
using Windows.UI;

namespace SeriesManager.Model.Banner
{
    public sealed class FanArtBannerModel : BannerBaseModel
    {
        #region Properties

        private int width;
        private int height;
        private bool hasSeriesName;
        private string thumbnailLocalPath;
        private string thumbnailRemotePath;
        private bool thumbnailLoading;
        private bool thumbLoaded;

        public bool ThumbnailLoading
        {
            get
            {
                return this.thumbnailLoading;
            }
            private set
            {
                base.SetProperty(ref this.thumbnailLoading, value);
            }
        }

        public int Width
        {
            get
            {
                return this.width;
            }
            set
            {
                base.SetProperty(ref this.width, value);
            }
        }

        public int Height
        {
            get
            {
                return this.height;
            }
            set
            {
                base.SetProperty(ref this.height, value);
            }
        }

        public bool HasSeriesName
        {
            get
            {
                return this.hasSeriesName;
            }
            set
            {
                base.SetProperty(ref this.hasSeriesName, value);
            }
        }

        public string ThumbnailRemotePath
        {
            get
            {
                return this.thumbnailRemotePath;
            }
            set
            {
                base.SetProperty(ref this.thumbnailRemotePath, value);
            }
        }

        public string ThumbnailLocalPath
        {
            get
            {
                TDwl();
                return this.thumbnailLocalPath;
            }
            set
            {
                base.SetProperty(ref this.thumbnailLocalPath, value);
            }
        }

        public ObservableCollection<Color> Colors
        {
            get;
            private set;
        }

        #endregion

        #region Contructor

        public FanArtBannerModel(int seriesID) : base(seriesID)
        {
            this.Colors = new ObservableCollection<Color>();
        }

        public FanArtBannerModel(int seriesID, string remotePath) : this(seriesID)
        {
            base.RemotePath = remotePath;
        }

        #endregion

        private void TDwl()
        {
            if (!this.thumbLoaded && !this.thumbnailLoading)
            {
                this.ThumbnailLoading = true;
                Download(this.thumbnailRemotePath, true).ContinueWith(T =>
                {
                    try
                    {
                        this.ThumbnailLocalPath = T.Result;
                        this.thumbLoaded = true;
                    }
                    catch
                    {
                        this.thumbLoaded = false;
                    }
                    this.ThumbnailLoading = false;
                }, TaskContinuationOptions.ExecuteSynchronously);
            }
        }

        public async Task DownloadThumbnail()
        {
            if (!this.thumbLoaded && !this.thumbnailLoading)
            {
                this.ThumbnailLoading = true;
                try
                {
                    this.ThumbnailLocalPath = await Download(this.thumbnailRemotePath, true);
                    this.thumbLoaded = true;
                }
                catch 
                { 
                    this.thumbLoaded = false; 
                }
                this.ThumbnailLoading = false;
            }    
        }
    }
}
